package top.fengyulou.xiaofeng.service;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.fengyulou.xiaofeng.common.UuidUtil;
import top.fengyulou.xiaofeng.dto.ClassifyDto;
import top.fengyulou.xiaofeng.dto.PaperDto;
import top.fengyulou.xiaofeng.dto.PaperQuestionRuleDto;
import top.fengyulou.xiaofeng.dto.QuestionDto;
import top.fengyulou.xiaofeng.enums.PaperQuestionRuleTypeEnum;
import top.fengyulou.xiaofeng.mapper.PaperQuestionRuleMapper;

import javax.annotation.Resource;
import java.util.*;

/**
 * 分类
 *
 * @author guochao
 * @version 1.0
 * @date 2022/2/2 18:47
 */
@Service
public class PaperQuestionRuleService {

    @Resource
    private PaperQuestionRuleMapper paperQuestionRuleMapper;
    @Resource
    private ClassifyService classifyService;
    @Resource
    private LabelService labelService;
    @Resource
    private PaperService paperService;

    /**
     * 列表查询
     */
    public void listPage(PaperQuestionRuleDto query) {
        //查询数据
        PageHelper.startPage(query.getPageNumber(), query.getPageSize());
        List<PaperQuestionRuleDto> paperQuestionRuleList = paperQuestionRuleMapper.list(query);
        PageInfo<PaperQuestionRuleDto> pageInfo = new PageInfo<>(paperQuestionRuleList);

        //返回结果
        query.setTotal(pageInfo.getTotal());
        query.setRows(paperQuestionRuleList);
    }

    //列表
    public List<PaperQuestionRuleDto> list(PaperQuestionRuleDto query) {
        return paperQuestionRuleMapper.list(query);
    }

    /**
     * id查询
     */
    public PaperQuestionRuleDto getById(String id) {
        PaperQuestionRuleDto paperQuestionRuleDto = paperQuestionRuleMapper.getById(id);
        return paperQuestionRuleDto;
    }

    //保存
    public void save(PaperQuestionRuleDto paperQuestionRuleDto) {
        if (StringUtils.isNotBlank(paperQuestionRuleDto.getId())) {
            paperQuestionRuleDto.setUpdateTime(new Date());
            paperQuestionRuleDto.setLastUpdateUserId(paperQuestionRuleDto.getCurrentUserId());
            paperQuestionRuleMapper.update(paperQuestionRuleDto);
        } else {
            paperQuestionRuleDto.setId(UuidUtil.getShortUuid());
            paperQuestionRuleDto.setCreateTime(new Date());
            paperQuestionRuleDto.setCreateUserId(paperQuestionRuleDto.getCurrentUserId());
            paperQuestionRuleMapper.insert(paperQuestionRuleDto);
        }
    }

    //保存
    public void update(PaperQuestionRuleDto paperQuestionRuleDto) {
        paperQuestionRuleDto.setUpdateTime(new Date());
        paperQuestionRuleDto.setLastUpdateUserId(paperQuestionRuleDto.getCurrentUserId());
        paperQuestionRuleMapper.update(paperQuestionRuleDto);
    }

    public void insertBatch(PaperQuestionRuleDto paperQuestionRuleDto) {
        String[] arr = paperQuestionRuleDto.getClassifyIds().split(",");
        List<PaperQuestionRuleDto> list = new ArrayList<>();
        paperQuestionRuleDto.setCreateTime(new Date());
        Arrays.stream(arr).forEach(classifyId -> {
            PaperQuestionRuleDto pqr = new PaperQuestionRuleDto();
            pqr.setId(UuidUtil.getShortUuid());
            pqr.setPaperId(paperQuestionRuleDto.getPaperId());
            pqr.setClassifyId(classifyId);
            pqr.setType(0);
            pqr.setQstType(paperQuestionRuleDto.getQstType());
            pqr.setCreateTime(paperQuestionRuleDto.getCreateTime());
            pqr.setCreateUserId(paperQuestionRuleDto.getCurrentUserId());
            list.add(pqr);
        });
        paperQuestionRuleMapper.insertBatch(list);
    }

    //删除
    public void delete(String[] ids) {
        paperQuestionRuleMapper.delete(ids);
    }

    //删除
    @Transactional
    public void deleteExtend(PaperQuestionRuleDto paperQuestionRuleDto) {
        //删除试卷规则
        paperQuestionRuleMapper.delete(paperQuestionRuleDto.getIds());
        //修改试卷试题和试卷总分数
        PaperDto paperDto = new PaperDto();
        paperDto.setId(paperQuestionRuleDto.getPaperId());
        paperDto.setCurrentUserId(paperQuestionRuleDto.getCurrentUserId());
        paperService.updateRuleQstNum(paperDto);
        paperService.updateRuleQstScore(paperDto);
    }

    public List<String> getClassifyIdListByPaperId(PaperQuestionRuleDto paperQuestionRuleDto) {
        return paperQuestionRuleMapper.getClassifyIdListByPaperId(paperQuestionRuleDto);
    }

    public Map<String,Object> getTypeCountByPaperId(String paperId) {
        List<PaperQuestionRuleDto> typeCountList= paperQuestionRuleMapper.getTypeCountByPaperId(paperId);
        long typeA=0;
        long typeB=0;
        long typeC=0;
        long typeD=0;
        if (ObjectUtils.isNotEmpty(typeCountList)) {
            for (PaperQuestionRuleDto paperQuestionRuleDto : typeCountList) {
                //单选题数量
                if (paperQuestionRuleDto.getQstType().equals("A")) {
                    typeA=paperQuestionRuleDto.getTypeQstNum();
                }
                //多选题数量
                else if(paperQuestionRuleDto.getQstType().equals("B")){
                    typeB=paperQuestionRuleDto.getTypeQstNum();
                }
                //判断题数量
                else if(paperQuestionRuleDto.getQstType().equals("C")){
                    typeC=paperQuestionRuleDto.getTypeQstNum();
                }
                //主观题数量
                else if(paperQuestionRuleDto.getQstType().equals("D")){
                    typeD=paperQuestionRuleDto.getTypeQstNum();
                }
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("typeA",typeA);
        map.put("typeB",typeB);
        map.put("typeC",typeC);
        map.put("typeD",typeD);
        return map;
    }

    //根据试卷id、分类id、试题类型查询
    public PaperQuestionRuleDto getByPaperIdAndClassifyIdAndQstType(String paperId, String classifyId, String qstType) {
        return paperQuestionRuleMapper.getByPaperIdAndClassifyIdAndQstType(paperId,classifyId,qstType);
    }

    //修改随机试卷试题数量
    @Transactional
    public void updateRuleQstNum(PaperQuestionRuleDto paperQuestionRuleDto) {
        update(paperQuestionRuleDto);
        PaperDto paperDto = new PaperDto();
        paperDto.setId(paperQuestionRuleDto.getPaperId());
        paperDto.setCurrentUserId(paperQuestionRuleDto.getCurrentUserId());
        paperService.updateRuleQstNum(paperDto);
    }

    //修改随机试卷试题分数
    @Transactional
    public void updateRuleQstScore(PaperQuestionRuleDto paperQuestionRuleDto) {
        update(paperQuestionRuleDto);
        PaperDto paperDto = new PaperDto();
        paperDto.setId(paperQuestionRuleDto.getPaperId());
        paperDto.setCurrentUserId(paperQuestionRuleDto.getCurrentUserId());
        paperService.updateRuleQstScore(paperDto);
    }
}

